package com.hy.dao.impl;
/*
 * @ Date: 2024/5/14 22:33
 * @ Description: OrderstableDaoImpl
 **/

import com.hy.bean.Orderstable;
import com.hy.dao.OrderstableDao;
import com.hy.exception.BizException;
import com.hy.utils.DBManager;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class OrderstableDaoImpl implements OrderstableDao {
    private QueryRunner qr = new QueryRunner();
    private SimpleDateFormat sdf = new SimpleDateFormat();
    @Override
    public List<Map<String, Object>> select(String orderId, String beginDate, String endDate, String creator){
        Connection conn = null;
        try {
            conn = DBManager.getConnection();
        } catch (Exception e) {
            throw new BizException("建立连接出错啦",e);
        }
        StringBuilder sb = new StringBuilder("select o.*,c.clientName clientname , e.employeeName employeename " +
                " from orderstable o " +
                " inner join clienttable c on c.id = o.client_id " +
                " inner join employeetable e on e.id = o.creator ");
        List<Object> paramList = new ArrayList<>();
        boolean isWhere = true;

        if (!StringUtils.isBlank(orderId)) {
            if (isWhere){
                sb.append(" where ");
                isWhere = false;
            } else {
                sb.append(" or ");
            }

            sb.append("orderId like ? ");
            paramList.add("%"+orderId+"%");
        }

        if (!StringUtils.isBlank(beginDate) && !StringUtils.isBlank(endDate)) {
            if (isWhere){
                sb.append(" where ");
                isWhere = false;
            } else {
                sb.append(" or ");
            }
            sb.append(" (createTime >= ? and createTime<= ?)");

            paramList.add(beginDate);
            paramList.add(endDate);

        }else if(!StringUtils.isBlank(beginDate) && StringUtils.isBlank(endDate)){
            if (isWhere){
                sb.append(" where ");
                isWhere = false;
            } else {
                sb.append(" or ");
            }

            sb.append(" (createTime >= ? )");
            paramList.add(beginDate);
        } else if(!StringUtils.isBlank(beginDate) && StringUtils.isBlank(endDate)){
            if (isWhere){
                sb.append(" where ");
                isWhere = false;
            } else {
                sb.append(" or ");
            }

            sb.append(" (partCreateDate<= ?) ");
            paramList.add(endDate);
        }

        if (!StringUtils.isBlank(creator)){
            if (isWhere){
                sb.append(" where ");
                isWhere = false;
            } else {
                sb.append(" or ");
            }
            sb.append(" creator = ? ");
            paramList.add(creator);
        }

        String sql = sb.toString();
        System.out.println("sql ==================>" + sql);

        List<Map<String, Object>> mapList = null ;
        try{
            if (paramList.size() >=1 )
                mapList =  qr.query(conn, sql, new MapListHandler(), paramList.toArray());
            else
                mapList = qr.query(conn,sql, new MapListHandler());
        }catch (Exception e){
            throw new BizException("查询出错啦",e);
        }finally {
            try {
                DbUtils.close(conn);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

//        mapList.forEach(m->{
//            m.put("reachTime",sdf.format(m.get("reachTime")));
//        });

        return mapList;
    }

    @Override
    public List<Map<String, Object>> select(){
        Connection conn = null;
        try {
            conn = DBManager.getConnection();
            String sql = "select o.*,c.clientName clientname,e.employeeName employeename from orderstable o inner join clienttable c on c.id = o.client_id inner join employeetable e on e.id = o.creator ";
            List<Map<String, Object>> mapList = qr.query(conn, sql, new MapListHandler());

//            mapList.forEach(m->{
////                m.put("reachTime",sdf.format(m.get("reachTime")));
//                System.out.println(m.get("reachTime"));
//            });

            return mapList;
        } catch (Exception e) {
            throw new BizException("Dao出错啦",e);
        }finally {
            try {
                DbUtils.close(conn);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }

    }

    public static void main(String[] args) {
//        System.out.println(new OrderstableDaoImpl().select("2","","","2"));
//        System.out.println(new OrderstableDaoImpl().select("","2000-1-1","2000-9-1","2"));
//        System.out.println(new OrderstableDaoImpl().select("","","","10"));

        System.out.println(new OrderstableDaoImpl().select());
    }
    @Override
    public Orderstable queryById(Integer id) {
        return null;
    }

    @Override
    public List<Orderstable> queryAllByLimit() {
        return null;
    }

    @Override
    public long count(Orderstable orderstable) {
        return 0;
    }

    @Override
    public int insert(Orderstable orderstable) {
        return 0;
    }

    @Override
    public int update(Orderstable orderstable) {
        return 0;
    }

    @Override
    public int deleteById(Integer id) {
        return 0;
    }
}
